package xju.springboot_17_18.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xju.springboot_17_18.entity.Book;
import xju.springboot_17_18.entity.Friend;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("user")
public class UserController {

    @Value("${friend.name}")
    private String name;
    @Value("${friend.age}")
    private int age;
    @Value("${pets[0]}")
    private List<String> pets;

    @Autowired
    private Environment environment;

    @Autowired
    private Friend friend;


    @GetMapping
    @ResponseBody
    public String index() {

        System.out.println("name "+name);
        System.out.println("age "+age);
        System.out.println("pets "+pets);

        System.out.println(environment.getProperty("friend.name"));
        System.out.println(environment.getProperty("friend.age"));
        System.out.println(environment.getProperty("pets[0]"));

        System.out.println(friend);
        return "index";
    }

    @GetMapping("jsp")
    public String jsp() {
        System.out.println(123);
        return "list";
    }

    @GetMapping("html")
    public String html(Model model) {
        System.out.println("templates");
        model.addAttribute("price", 9.99);
        model.addAttribute("str", "西游记");
        Book book = new Book();
        book.setId(1);
        book.setTitle("西游记");
        book.setPrice(30.0);
        book.setAuthor("wu");
        Book book1 = new Book();
        book1.setId(2);
        book1.setTitle("水浒传");
        book1.setPrice(40.0);
        book1.setAuthor("luo");
        List<Book> books = new ArrayList<>();
        books.add(book);
        books.add(book1);
        model.addAttribute("book", book);
        model.addAttribute("color", "red");
        model.addAttribute("books", books);
        return "test";
    }

    @GetMapping("main")
    public String main() {
        System.out.println(333);
        return "main";
    }
}
